import util from '@/libs/util';
import Cookies from 'js-cookie';
export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                type: 'index'
            }, {
                title: '教室名称',
                key: 'name'
            }, {
                title: '所在楼名',
                key: 'bulidName'
            }, {
                title: '座位数量',
                key: 'setAmount'
            }, {
                title: '是否启用',
                key: 'status',
                render: function (h, params) {
                    let text = '';
                    if (params.row.status == 1) {
                        text = '启用';
                    } else if (params.row.status == 2) {
                        text = '禁用';
                    }
                    return h('div', {}, text);
                }
            }, {
                title: '操作',
                key: 'action',
                width: 300,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        // h('Button', {
                        //     props: {
                        //         type: 'primary',
                        //         size: 'small'
                        //     },
                        //     style: {
                        //         marginRight: '5px'
                        //     },
                        //     on: {
                        //         click: () => {
                        //             this.detail(params.row.id);
                        //         }
                        //     }
                        // }, '查看'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showAdd(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    this.doDel(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            dataList: [],
            addVisible: false, // 新增弹窗的显示和隐藏
            isRead: false, // 新增、编辑或查看时，数据是否可以修改
            formData: {}, // 新增/编辑表单数据
            queryData: {}, // 查询表单数据
            entityData: {}, // 查看详情的数据
            pageSize: 5, // 分页数据
            pageIndex: 1, // 分页数据
            total: 0,
            baseuser: JSON.parse(Cookies.get('baseUser'))
        };
    },
    mounted () {
        this.init();
    },
    methods: {
        init: function () { // 页面初始化
            this.doQuery();
        },
        doQuery: function () {
            var queryData = this.queryData;
            queryData.page = this.pageIndex;
            queryData.size = this.pageSize;
            util.ajax.post('/base/classroom/list', util.toFormData(queryData)).then(res => {
                if (res.data.data) {
                    this.dataList = res.data.data.list;
                    this.total = res.data.data.total;
                }
            });
        },
        showAdd: function (obj) {
            this.formData = obj;
            this.addVisible = true;
        },
        doDel: function (id) {
            if (id) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '确认删除？',
                    onOk: () => {
                        util.ajax.post('/base/classroom/delete', util.toFormData({id: id})).then(res => {
                            if (res.data.code == 200) {
                                if (res.data.message == 'SUCCESS') {
                                    this.$Message.success('删除成功！');
                                    this.pageIndex = 1;
                                    this.init();
                                } else {
                                    this.$Message.warning(res.data.message || '未知错误！');
                                }
                            }
                        });
                    },
                    onCancel: () => {

                    }
                });
            } else {
                this.$Modal.error({content: '请选择删除项!'});
            }
        },
        doSave: function () {
            let formData = {};
            if (this.formData != null && this.formData != '' && this.formData != undefined) {
                for (var key in this.formData) {
                    formData[key] = this.formData[key];
                    if (key == 'name' || key == 'status') {
                        if (formData[key] == null || formData[key] == '' || formData[key] == undefined) {
                            this.$Modal.error({content: '带*为必填项，请检查！'});
                            return;
                        }
                    }
                }
                util.ajax.post('/base/classroom/save', util.toFormData(formData)).then(res => {
                    if (res.data.code == 200) {
                        if (res.data.message == 'SUCCESS') {
                            this.$Message.success('保存成功！');
                            this.addVisible = false;
                            this.pageIndex = 1;
                            this.init();
                        } else {
                            this.$Message.warning(res.data.message || '未知错误！');
                            this.addVisible = false;
                        }
                        this.formData = {};
                    }
                });
            } else {
                this.$Modal.error({content: '带*为必填项，请检查！'});
            }
        },
        whenSelectRow: function (currentRow, oldCurrentRow) {
            var id = currentRow.id;
            this.entityData.id = id;
        }
    }
};
